<?php

namespace app\common\model;

use think\Exception;
use think\Model;

class PyInstitution extends BaseModel
{
    //绑定管理员
    public function bindAdmin(){
        return $this->hasOne("CmsAdmin","adm_id","ins_admin_id");
    }
    //绑定城市
    public function bindCity(){
        return $this->hasOne("PyCity","cty_id","ins_city_id");
    }

    //机构编辑
    public function edit($data,$ins_id){
        //校验机构位置是否选择
        if($data['ins_longitude']+$data['ins_latitude']+$data['ins_zoom']==0){
            return "请选择坐标";
        }

        //校验机构名是否存在
        $ins_name=$data['ins_name'];
        $issetCondition=[];
        $issetCondition['ins_name']=['eq',$ins_name];
        $issetCondition['ins_id']=['neq',$ins_id];
        $isset=$this->where($issetCondition)->find();
        if($isset){
            return "该机构名已存在";
        }

        //数据过滤
        $data=$this->dataFilter($data);
        try{
            $result=$this->where("ins_id","=",$ins_id)->update($data);
        }catch (Exception $exception){
            return "数据编辑异常";
        }
        if(!$result){
            return "数据未发生变化";
        }
        return 1;
    }

    //设置默认图片读取
    public function getInsThumbAttr($value){
    if($value==""){
        $webInfo=cache("webInfo");
        return $webInfo['web_institution_default_image'];
    }else{
        return $value;
    }
}
}
